<?php
class Reports extends MY_Controller
{
	var $curl = 'reports';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
			
	function __construct()
	{
		parent::__construct();		

		if (!$this->auth->is_logged_in())
		{
			redirect('/', 'refresh');
		}
		
		$this->load->model(array('/public/reports_model'));

		$this->validation->set_fields($this->reports_model->fields);

		//*$this->data['Title'] = T_('Рекламные ролики');

		$this->language = parent::getLang();			
		
		
		
	}
	
	function index()
	{
		/*$this->advert_model->search();
		$this->search = $this->data['search'];
		$this->per_page = $this->data['per_page'];	*/
		$sql = "SELECT r.*,c.first_name,c.last_name,c.email,e.job,t.name AS test_name
				FROM reports r
				LEFT JOIN candidates c ON c.id=r.id_candidates
				LEFT JOIN tests_events e ON e.id=r.id_events
				LEFT JOIN tests t ON t.id=r.id_tests
				WHERE r.status=1 AND r.id_companies=".$this->auth->getID();
		$this->data['list'] = $this->db->query($sql)->result();

		if ($this->input->get('excel') == true)
		{
			$this->load->library('excel');  
			$this->load->view('/reports/excel', array('list' => $this->data['list']));	
		}
		
		$this->renderLayout();
	}
	
	function archive()
	{
		/*$this->advert_model->search();
		$this->search = $this->data['search'];
		$this->per_page = $this->data['per_page'];	*/	
		$sql = "SELECT r.*,c.first_name,c.last_name,c.email,e.job,t.name AS test_name
				FROM reports r
				LEFT JOIN candidates c ON c.id=r.id_candidates
				LEFT JOIN tests_events e ON e.id=r.id_events
				LEFT JOIN tests t ON t.id=r.id_tests
				WHERE r.status=2 AND r.id_companies=".$this->auth->getID();
		$this->data['list'] = $this->db->query($sql)->result();

		if ($this->input->get('excel') == true)
		{
			$this->load->library('excel');  
			$this->load->view('/reports/excel', array('list' => $this->data['list']));	
		}
		
		$this->renderLayout();
	}
		
	function common()
	{ 
		$this->data['Title'] = T_('Общий отчет');
		$this->name = 'Общий отчет';
		
		$this->reports_model->search();
		$this->search = $this->data['search'];
		$this->per_page = $this->data['per_page'];		
		$this->renderLayout();
	}
	
	function quantitive()
	{ 
		$this->data['Title'] = T_('Количественные отчеты');
		$this->name = 'Количественные отчеты';
		
		$this->reports_model->search1();
		$this->search = $this->data['search'];
		$this->per_page = $this->data['per_page'];		
		$this->renderLayout();
	}
	
	function finance()
	{ 
		$this->data['Title'] = T_('Финансовые отчеты');
		$this->name = 'Финансовые отчеты';
		
		$this->reports_model->search2();
		$this->data['chart'] = TRUE;
		//pr($this->data['list']);
		$this->renderLayout();
	}

	function getFinanceXML()
	{ 
		$this->reports_model->search2();
		
		$this->layout = 'ajax';
		$this->view = '/reports/xml/finance';
		//$this->data['list'] = null;
		$this->renderLayout();
	}
	
	function edit($id = null)
	{		
		if (!empty($id))
		{
			$object = $this->advert_model->get($id);
			$this->data['Title'] = $object['name'] . ' / ' . T_('Редактировать');
			$this->data['advert'] = $object;
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 				= $object['id'];
				$this->validation->id_advertisers 	= $object['id_advertisers'];
				$this->validation->name 			= $object['name'];
				$this->validation->description 		= $object['description'];
				$this->validation->duration 		= $object['duration'];
				$this->validation->status 			= $object['status'];
				$this->validation->refusing 		= $object['refusing']; 
			}
			else
			{
				$rules = array('name', 'description', 'duration', 'status', 'refusing');
				$this->validation->set_rules($this->advert_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 		= $this->validation->id;
					$item['name'] 		= $this->validation->name;
					$item['description']= $this->validation->description;
					$item['duration'] 	= $this->validation->duration;					
					$item['status'] 	= $this->validation->status;
					$item['refusing'] 	= $this->validation->refusing;				
					
					$this->advert_model->update($item);
					
					redirect($this->curl . '/index');				
				}
				else
				{
					$this->form = $this->curl . '/edit';
				}				
			}			
		}
		
		$this->advert_model->search();
		$this->renderLayout();
	}
		
	function cancelItems()
	{
		$data = $this->advert_model->cancel($_REQUEST['items']);

		echo $this->utils->php2js($data);
	}

	function cancelItem($ID)
	{
		$data = $this->advert_model->cancel($ID);

		redirect($this->curl . '/index');
	}
	
	function xml() 
	{
		 header ("content-type: text/xml");
		 
		$text = '
		<chart>
   <chart_data>
      <row>
         <null/>
         <string>2000</string>
         <string>2007</string>
         <string>2008</string>
         <string>2009</string>
      </row>
      <row>
         <string>Region A</string>
         <number>5</number>
         <number>10</number>
         <number>30</number>
         <number>63</number>
      </row>
      <row>
         <string>Region B</string>   
         <number>100</number>
         <number>20</number>
         <number>65</number>
         <number>55</number>
      </row>
      <row>
         <string>Region C</string>
         <number>56</number>
         <number>21</number>
         <number>5</number>
         <number>90</number>
      </row>
   </chart_data>
   
</chart>';
		
		echo $text;
	}
	
  function archiveItems()
  {
    $data = $this->reports_model->archive($_REQUEST['items']);

    echo $data;
  }
}
?>